/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.product.web.catalog;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.Message;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.product.entity.catalog.GenCatalog;
import com.thinkgem.jeesite.modules.product.entity.product.GenProduct;
import com.thinkgem.jeesite.modules.product.service.catalog.GenCatalogService;
import com.thinkgem.jeesite.modules.product.service.product.GenProductService;

/**
 * 商品类别Controller
 * @author Egan
 * @version 2016-12-12
 */
@Controller
@RequestMapping(value = "${adminPath}/product/catalog/genCatalog")
public class GenCatalogController extends BaseController {

	@Autowired
	GenCatalogService genCatalogService;
	
	@Autowired
	GenProductService genProductService;
	
	@ModelAttribute
	public GenCatalog get(@RequestParam(required=false) String id) {
		GenCatalog entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = genCatalogService.get(id);
		}
		if (entity == null){
			entity = new GenCatalog();
		}
		return entity;
	}
	
	@RequiresPermissions("product:catalog:genCatalog:view")
	@RequestMapping(value = {"list", ""})
	public String list(GenCatalog genCatalog, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		Page<GenCatalog> root = genCatalogService.loadRoot(new Page<GenCatalog>(request, response), genCatalog);
		List<GenCatalog> result = Lists.newArrayList();
		for(GenCatalog cata : root.getList()){
			appendChildren(cata, result);
		}
		root.setList(result);
		request.setAttribute("page", root);
		
		return "modules/product/catalog/genCatalogList";
	}
	
	private void appendChildren(GenCatalog catalog, List<GenCatalog> list) {
		if(catalog == null){
			return;
		}
		list.add(catalog);
		List<GenCatalog> nextLevelList = genCatalogService.getNextLevelStep(catalog.getId());
		for (GenCatalog cata: nextLevelList) {
			appendChildren(cata, list);
			cata.setChildren(getChild(cata));
		}
	}
	
	private List<GenCatalog> getChild(GenCatalog cata) {
		
		List<GenCatalog> nextLevelList = genCatalogService.getNextLevelStep(cata.getId());
		for (int i = 0; i < nextLevelList.size(); i ++) {
			cata.setChildren(getChild(nextLevelList.get(i)));
		}
		return nextLevelList;
	}
	
	@RequiresPermissions("product:catalog:genCatalog:view")
	@RequestMapping(value = "form")
	public String form(GenCatalog genCatalog, Model model) {
		model.addAttribute("genCatalog", genCatalog);
		List<GenCatalog> rootCatalogs = genCatalogService.findList(null);
		model.addAttribute("rootCatalogs", rootCatalogs);
		return "modules/product/catalog/genCatalogForm";
	}

	@RequiresPermissions("product:catalog:genCatalog:edit")
	@RequestMapping(value = "save")
	@Transactional
	public String save(GenCatalog genCatalog, Model model, RedirectAttributes redirectAttributes) {
		try {
			/*if (!beanValidator(model, genCatalog)){
				return form(genCatalog, model);
			}*/
			genCatalogService.save(genCatalog);
			addMessage(redirectAttributes, Message.getContent("genCatalog.success"));
		} catch (Exception e) {
			addMessage(redirectAttributes, Message.getContent("genCatalog.data.exists"));
		}
		
		return "redirect:"+Global.getAdminPath()+"/product/catalog/genCatalog/?repage";
	}
	
	@RequiresPermissions("product:catalog:genCatalog:edit")
	@RequestMapping(value = "delete")
	@Transactional
	public String delete(GenCatalog genCatalog, RedirectAttributes redirectAttributes) {
		try {
			List<GenCatalog> genCatalogs = genCatalogService.getNextLevelStep(genCatalog.getId());
			
			if (genCatalogs == null || genCatalogs.size() == 0) {
				List<GenProduct> products = genProductService.findListByCatalogIdTrue(genCatalog.getId());
				if (products == null || products.size() == 0) {
					genCatalogService.delete(genCatalog);
					addMessage(redirectAttributes,  Message.getContent("genCatalog.delete.success"));
				} else {
					addMessage(redirectAttributes, Message.getContent("genCatalog.data.exist.product"));
				}
			}
			else {
				addMessage(redirectAttributes, Message.getContent("genCatalog.data.exist.parent"));
			}
		} catch (Exception e) {
			addMessage(redirectAttributes, Message.getContent("genCatalog.data.exists"));
		}
		return "redirect:"+Global.getAdminPath()+"/product/catalog/genCatalog/?repage";
	}

}